package com.sg.business.commons.ui.vault;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.util.Util;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.rap.rwt.RWT;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import com.mobnut.commons.util.Utils;
import com.sg.business.model.Folder;
import com.sg.business.resource.BusinessResource;
import com.sg.widgets.Widgets;

public class FolderPathCrumb extends Composite implements ISelectionProvider {

	private List<Folder> path;

	private List<Button> buttons;

	private ListenerList listeners;

	private StructuredSelection selection;

	public FolderPathCrumb(Composite parent) {
		super(parent, SWT.NONE);
		setBackground(Widgets.getColor(getDisplay(), 0xcd, 0xcd, 0xcd));
		path = new ArrayList<Folder>();
		buttons = new ArrayList<Button>();
		RowLayout layout = new RowLayout(SWT.HORIZONTAL);
		layout.wrap = false;
		layout.pack = true;
		layout.justify = false;
		layout.spacing = 0;
		layout.marginTop = 0;
		layout.marginLeft = 0;
		layout.marginRight = 1;
		setLayout(layout);
		addFolder(null);
	}

	public void addFolder(final Folder folder) {
		if (path.size() > 0) {
			Folder lastFolder = path.get(path.size() - 1);
			if (lastFolder == null && folder == null) {
				return;
			} else if (Util.equals(lastFolder, folder)) {
				return;
			}
		}

		path.add(folder);
		Button button = new Button(this, SWT.PUSH);
		button.setData(RWT.CUSTOM_VARIANT, "smallpadding");
		String buttonLabel;
		String toolTips = null;
		Image image = null;
		if (folder == null) {
			buttonLabel = "/";
			image = BusinessResource.getImage(BusinessResource.IMAGE_DATABASE_16);
		} else {
			String desc = folder.getDesc();
			buttonLabel = "/" + Utils.getLimitLengthString2(desc, 20);
			toolTips = desc;
		}
		button.setText(buttonLabel);
		if (image != null) {
			button.setImage(image);
		}
		if (toolTips != null) {
			button.setToolTipText(toolTips);
		}
		RowData rd = new RowData();
		rd.height = 24;
		button.setLayoutData(rd);
		layout();

		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				removeFolder(folder);
			}
		});
		buttons.add(button);
		selectFolder(folder);
	}

	private void removeFolder(Folder folder) {
		int index = path.indexOf(folder);
		if (index != -1) {
			for (int i = index + 1; i < buttons.size(); i++) {
				buttons.get(i).dispose();
				buttons.remove(i);
				path.remove(i);
				i--;
			}
		}
		layout();
		selectFolder(folder);
	}

	private void selectFolder(Folder folder) {
		if (folder != null) {
			this.selection = new StructuredSelection(new Object[] { folder });
		} else {
			this.selection = StructuredSelection.EMPTY;
		}
		fireSelectionsChanged(new SelectionChangedEvent(this, selection));
	}

	@Override
	public void addSelectionChangedListener(ISelectionChangedListener listener) {
		if (listeners == null) {
			listeners = new ListenerList();
		}
		listeners.add(listener);
	}

	private void fireSelectionsChanged(SelectionChangedEvent event) {
		if (listeners != null) {
			Object[] lis = listeners.getListeners();
			for (int i = 0; i < lis.length; i++) {
				((ISelectionChangedListener) lis[i]).selectionChanged(event);
			}
		}
	}

	@Override
	public ISelection getSelection() {
		return selection;
	}

	@Override
	public void removeSelectionChangedListener(
			ISelectionChangedListener listener) {
		if (listeners != null) {
			listeners.remove(listener);
		}
	}

	@Override
	public void setSelection(ISelection selection) {
		// if(selection==null||selection.isEmpty()){
		//
		// }
	}

	public void clean() {
		for (int i = 1; i < buttons.size(); i++) {
			buttons.get(i).dispose();;
			buttons.remove(i);
			path.remove(i);
			i--;
		}
		layout();
	}

}
